package com.depthsdata.coe.api;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import com.depthsdata.coe.dao.BusinessProcessDiagramDAOQueryBean;
import com.depthsdata.coe.service.BusinessProcessDiagramBean;
import com.depthsdata.coe.service.IBusinessProcessService;
import com.ourteam.frame.controller.AbstractCRUDServiceController;

@Controller
@CrossOrigin
@RequestMapping("/api/COE/businessProcessDiagram")
public class BusinessProcessDiagramManageAPI extends
		AbstractCRUDServiceController<BusinessProcessDiagramDAOQueryBean, BusinessProcessDiagramSearchBean, BusinessProcessDiagramBean> {

	@Autowired
	private IBusinessProcessService businessProcessService;

	@Override
	protected BusinessProcessDiagramDAOQueryBean newQueryBean() {
		return new BusinessProcessDiagramDAOQueryBean();
	}

	@Override
	protected int queryCount(BusinessProcessDiagramDAOQueryBean queryBean) throws Exception {
		return businessProcessService.queryBusinessProcessDiagramCount(queryBean);
	}

	@Override
	protected BusinessProcessDiagramBean[] queryData(BusinessProcessDiagramDAOQueryBean queryBean) throws Exception {
		return businessProcessService.queryBusinessProcessDiagrams(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {

		businessProcessService.deleteBusinessProcessDiagrams(ids);

	}

	@Override
	protected BusinessProcessDiagramBean getDataById(long id) throws Exception {
		return businessProcessService.getBusinessProcessDiagramById(id);
	}

	@Override
	protected void saveData(BusinessProcessDiagramBean dataBean) throws Exception {

		BusinessProcessDiagramDAOQueryBean queryBean = new BusinessProcessDiagramDAOQueryBean();

		queryBean.createCriteria().andBusinessProcessIdEqualTo(dataBean.getBusinessProcessId());

		BusinessProcessDiagramBean[] diagramBeans = businessProcessService.queryBusinessProcessDiagrams(queryBean);

		if (diagramBeans.length == 0) {
			dataBean.setUpdateTime(new Date());
			businessProcessService.addNewBusinessProcessDiagram(dataBean);
		} else {
			diagramBeans[0].setUpdateTime(new Date());
			diagramBeans[0].setBusinessProcessDiagram(dataBean.getBusinessProcessDiagram());
			businessProcessService.modifyBusinessProcessDiagram(diagramBeans[0]);
		}

	}

}
